package com.example.home.api;

import android.util.Log;
import android.view.View;

import com.example.home.interfaces.ResponseCallback;
import com.example.home.viewmodel.GoodsList.GoodsListData;
import com.example.home.viewmodel.GoodsList.List;
import com.example.home.viewmodel.Login.LoginData;
import com.example.home.viewmodel.ResponseData;
import com.example.home.viewmodel.cart.CartData;
import com.example.home.viewmodel.classify.ClassifyData;
import com.example.home.viewmodel.home.HomeData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.Map;

public class ApiService {

    private static final String baseURL = "http://hmapp.net:8888";

    public static void GetHome(ResponseCallback callback){
        OkGo.<String>get(baseURL+"/wx/home/index").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<HomeData> result =new Gson().fromJson(
                        response.body(),new TypeToken<ResponseData<HomeData>>(){}.getType()
                );
                callback.onSuccess(result);
            }
        });
    }
    public static void GetClassify(ResponseCallback callback){
        OkGo.<String>get(baseURL+"/wx/catalog/index").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<ClassifyData> result = new Gson().fromJson(
                        response.body(),
                        new TypeToken<ResponseData<ClassifyData>>(){}.getType()
                );
                callback.onSuccess(result);
            }
        });
    }

    public static void GetCurrentClassify(String str,ResponseCallback callback){
        OkGo.<String>get(baseURL+"/wx/catalog/current?id="+str).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<ClassifyData> result = new Gson().fromJson(
                        response.body(),
                        new TypeToken<ResponseData<ClassifyData>>(){}.getType()
                );
                Log.e("TAG", "onSuccess: "+result );
                callback.onSuccess(result);
            }
        });
    }

    public static void GetGoodsNavication(String str,ResponseCallback callback){
        OkGo.<String>get(baseURL+"/wx/goods/category?id="+str).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<GoodsListData> result = new Gson().fromJson(
                        response.body(),
                        new TypeToken<ResponseData<GoodsListData>>(){}.getType()
                );
                Log.e("TAG", "onSuccess: "+result );
                callback.onSuccess(result);
            }
        });
    }

//    http://hmapp.net:8888/wx/goods/list?categoryId=1008004&page=1&limit=10
    public static void GetGoodsList(String str, ResponseCallback<ResponseData<GoodsListData>> callback){
        OkGo.<String>get(baseURL+"/wx/goods/list?categoryId="+str+"&page=1&limit=10").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<GoodsListData> result = new Gson().fromJson(
                        response.body(),
                        new TypeToken<ResponseData<GoodsListData>>(){}.getType()
                );
                Log.e("TAG", "onSuccess: "+result );
                if(result != null) {
                    callback.onSuccess(result);
                }
            }
        });
    }
    public static void Login(View view, Map<String, String> params, ResponseCallback<ResponseData<LoginData>> callback){
        OkGo.<String>post(baseURL+"/wx/auth/login")
                .tag(view.getContext())
                .upJson(new Gson().toJson(params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<LoginData> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<LoginData>>(){}.getType()
                        );
                        Log.e("TAG", "onSuccess: "+result );
                        if(result != null) {
                            callback.onSuccess(result);
                        }
                    }
                });
    }

    public static void GetCartList(String token, ResponseCallback<ResponseData<CartData>> callback){
        OkGo.<String>get(baseURL+"/wx/cart/index")
                .headers("x-litemall-token",token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CartData> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<CartData>>(){}.getType()
                        );
                        Log.e("TAG", "onSuccess: "+result );
                        if(result != null) {
                            callback.onSuccess(result);
                        }else{
                            return;
                        }
                    }
                });
    }
}
